package torchtools;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 *
 * @author Matt
 */
public class ItemList {
    
    private List<Item> items;
    
    protected static final Comparator<Item> ITEM_COMPARATOR = new Comparator<Item>() {
        public int compare(Item o1, Item o2) {
            return Strings.compareNatural(o1.toString(), o2.toString());
        }
    };
    
    public void load() throws IOException {
        load(null);
    }
    
    /** 
     * Throws a FileNotFoundException if masterresourceunits.dat has not
     * been generated.
     */
    public void load(String dataDir) throws IOException {
        if (dataDir==null) {
            //Tries to use the default data path, returns null if it can't be found
            String appdata = System.getenv("APPDATA");
            if (appdata==null)
                throw new IOException("can't find %appdata%");
            dataDir = appdata+"\\runic games\\torchlight";
        } else if (dataDir.endsWith("\\")||dataDir.endsWith("/"))
            dataDir = dataDir.substring(0, dataDir.length()-1);
        File f = new File(dataDir+"\\masterresourceunits.dat");
        if (!f.exists()) {
            throw new FileNotFoundException("cannot find masterresourceunits" +
                    ".dat -- run Torchlight first");
        }

        FileInputStream fin = new FileInputStream(f);
        InputStreamReader in = new InputStreamReader(fin, "UTF16");
        BufferedReader br = new BufferedReader(in);
        String line = "";

        items = new ArrayList<Item>(1000);
        final String PRENAME = "<STRING>NAME:";
        final String PREDISPLAYNAME = "<TRANSLATE>DISPLAYNAME:";
        final String PREUNITTYPE = "<STRING>UNITTYPE:";
        final String PREPATH = "<STRING>DATFILE:";
        final String PRESET = "<STRING>SET:";

        String name = null;
        String displayname = null;
        String path = null;
        String unittype = null;
        String set = null;
        while ( (line=br.readLine()) != null ) {
            line = line.trim();
            if (line.equals("[MONSTERS]")) {
                break;
            } else if (line.equals("[ITEMS]")) {
                name = null;
                displayname = null;
                path = null;
                unittype = null;
                set = null;
            } else if (line.equals("[/ITEMS]")) {
                if (displayname == null)
                    displayname = name;
                if (unittype!=null)
                    items.add(new Item(name, displayname, unittype, path, set));
            } else if (line.startsWith(PRENAME)) {
                if (name==null)
                    name = line.substring(PRENAME.length());
            } else if (line.startsWith(PRESET)) {
                set = line.substring(PRESET.length());
            } else if (line.startsWith(PREDISPLAYNAME)) {
                displayname = line.substring(PREDISPLAYNAME.length());
            } else if (line.startsWith(PREUNITTYPE)) {
                unittype = line.substring(PREUNITTYPE.length());
            } else if (line.startsWith(PREPATH)) {
                path = line.substring(PREPATH.length());
            }
        }
        br.close();
        in.close();
    }
    
    public void sort() {
        if (items!=null) {
            Collections.sort(items, ITEM_COMPARATOR);
        }
    }
    
}
